package com.wangjiyuan.giftshuo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.IconHintView;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.home.banners.BannersBean;
import com.wangjiyuan.giftshuo.bean.home.column.ItemsBean;
import com.wangjiyuan.giftshuo.bean.home.secondarybanners.SecondaryBannersBean;
import com.wangjiyuan.giftshuo.ui.activity.home.DetailActivity;
import com.wangjiyuan.giftshuo.utils.RecyclerViewAdapterHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wjy on 2016/11/17.
 */

public class HomeRecyclerAdapter extends RecyclerViewAdapterHelper<ItemsBean> {

    private int id;
    private List<BannersBean> adlist;
    private List<SecondaryBannersBean> secondarylist;

    /**
     * @param context
     * @param list
     * @param id      代表栏目id
     */
    public HomeRecyclerAdapter(Context context, List<ItemsBean> list,
                               List<BannersBean> adlist,
                               List<SecondaryBannersBean> secondarylist, int id) {
        super(context, list);
        this.id = id;
        this.adlist = adlist;
        this.secondarylist = secondarylist;
    }

    @Override
    public int getItemCount() {
        if (adlist != null) {
            if (mList.size() == 0) {
                return 0;
            } else {
                return mList.size();
            }
        }
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (id == 107 && position == 0) {
            return R.layout.item_banner;

        } else if (id == 107 && mList.get(position - 1).getAuthor() == null) {
            return R.layout.item_ad;

        } else {
            return R.layout.item_essay;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {

        View view;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case R.layout.item_banner:
                view = mInflater.inflate(viewType, parent, false);
                viewHolder = new BannerViewHolder(view);
                break;
            case R.layout.item_essay:
                view = mInflater.inflate(viewType, parent, false);
                viewHolder = new EssayViewHolder(view);
                break;
            case R.layout.item_ad:
                view = mInflater.inflate(viewType, parent, false);
                viewHolder = new ADViewHolder(view);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BannerViewHolder) {
            BannerViewHolder viewHolder = (BannerViewHolder) holder;
            setADBanners(viewHolder);
            setSecBanners(viewHolder);
        } else if (holder instanceof EssayViewHolder) {
            final EssayViewHolder viewHolder = (EssayViewHolder) holder;
            ItemsBean itemsBean;
            if (id != 107) {
                itemsBean = mList.get(position);
            } else {
                itemsBean = mList.get(position - 1);
            }

            viewHolder.setId(itemsBean.getId());
            setEssay(viewHolder, itemsBean);
        } else {
            ADViewHolder viewHolder = (ADViewHolder) holder;

            Glide.with(mContext)
                    .load(mList.get(position - 1).getImageUrl())
                    .asBitmap()
                    .placeholder(mContext.getResources().getDrawable(R.mipmap.post_place_holder_image))
                    .centerCrop()
                    .into(viewHolder.ad_image);
        }

    }

    private void setEssay(final EssayViewHolder viewHolder, ItemsBean itemsBean) {
        Glide.with(mContext)
                .load(itemsBean.getCoverImageUrl())
                .placeholder(mContext.getResources().getDrawable(R.mipmap.post_place_holder_image))
                .centerCrop()
                .into(viewHolder.cover_image);
        Glide.with(mContext)
                .load(itemsBean.getAuthor().getAvatarUrl())
                .asBitmap()
                .placeholder(mContext.getResources().getDrawable(R.mipmap.post_place_holder_image))
                .centerCrop()
                .into(new BitmapImageViewTarget(viewHolder.avatar) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        viewHolder.avatar.setImageDrawable(circularBitmapDrawable);
                    }
                });
        viewHolder.nickname.setText(itemsBean.getAuthor().getNickname());
        if (itemsBean.getAuthor().getIntroduction() == null) {
            viewHolder.introduction.setVisibility(View.GONE);
        } else {
            viewHolder.introduction.setText(itemsBean.getAuthor().getIntroduction());
        }

        viewHolder.title.setText(itemsBean.getTitle());
        viewHolder.description.setText(itemsBean.getIntroduction());
        if (itemsBean.getColumn() != null) {
            viewHolder.column.setText(itemsBean.getColumn().getTitle() + "");
        } else {
            viewHolder.column.setVisibility(View.GONE);
        }

        viewHolder.like_count.setText(itemsBean.getLikesCount() + "");
    }

    private void setSecBanners(BannerViewHolder viewHolder) {
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        for (int i = 0; i < secondarylist.size(); i++) {
            ImageView secimg = new ImageView(mContext);
            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
            layoutParams.setMargins(2, 2, 2, 2);
            layoutParams.width = width / 3 - 4;
            layoutParams.height = layoutParams.width * 285 / 375 - 4;
            secimg.setLayoutParams(layoutParams);
            Glide.with(mContext)
                    .load(secondarylist.get(i).getImageUrl())
                    .centerCrop().into(secimg);
            viewHolder.banner.addView(secimg);
        }
    }

    private void setADBanners(final BannerViewHolder viewHolder) {
        List<ImageView> adimgs = new ArrayList<>();
        for (int i = 0; i < adlist.size(); i++) {
            ImageView adimg = new ImageView(mContext);
            ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
            adimg.setLayoutParams(layoutParams);
            //加载网络图片
            Glide.with(mContext)
                    .load(adlist.get(i).getImageUrl())
                    .centerCrop().into(adimg);
            adimgs.add(adimg);
        }
        ADPagerAdapter adPagerAdapter = new ADPagerAdapter(viewHolder.ad_view_pager, adimgs);
        viewHolder.ad_view_pager.setAdapter(adPagerAdapter);
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        RollPagerView ad_view_pager;
        GridLayout banner;

        public BannerViewHolder(View itemView) {
            super(itemView);
            ad_view_pager = (RollPagerView) itemView.findViewById(R.id.ad_view_pager);
            ad_view_pager.setHintView(new IconHintView(mContext, R.drawable.dot_checked, R.drawable.dot_unchecked));
            banner = (GridLayout) itemView.findViewById(R.id.banner);
        }

    }

    class EssayViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView avatar;
        TextView nickname;
        TextView introduction;
        ImageView cover_image;
        TextView title;
        TextView description;
        TextView column;
        TextView like_count;
        LinearLayout essay_item;
        int id;

        public EssayViewHolder(View itemView) {
            super(itemView);
            avatar = (ImageView) itemView.findViewById(R.id.avatar);
            nickname = (TextView) itemView.findViewById(R.id.nickname);
            introduction = (TextView) itemView.findViewById(R.id.introduction);
            cover_image = (ImageView) itemView.findViewById(R.id.cover_image);
            int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
            ViewGroup.LayoutParams layoutParams = cover_image.getLayoutParams();
            layoutParams.width = widthPixels;
            layoutParams.height = layoutParams.width * 396 / 720;

            title = (TextView) itemView.findViewById(R.id.title);
            description = (TextView) itemView.findViewById(R.id.description);
            column = (TextView) itemView.findViewById(R.id.column);
            like_count = (TextView) itemView.findViewById(R.id.like_count);
            essay_item = (LinearLayout) itemView.findViewById(R.id.essay_item);
            essay_item.setOnClickListener(this);
            avatar.setOnClickListener(this);
            column.setOnClickListener(this);
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (v.getId()) {
                case R.id.essay_item:
                    intent.setClass(mContext, DetailActivity.class);
                    Bundle bundle = new Bundle();
                    List<Integer> list = new ArrayList<>();
                    for (ItemsBean bean : mList) {
                        list.add(bean.getId());
                    }
                    bundle.putInt("id", id);
                    bundle.putSerializable("ids", (Serializable) list);
                    bundle.putSerializable("items", (Serializable) mList);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                    break;
                case R.id.avatar:
                    break;

                case R.id.column:
                    break;
            }
        }
    }

    class ADViewHolder extends RecyclerView.ViewHolder {
        ImageView ad_image;

        public ADViewHolder(View itemView) {
            super(itemView);
            ad_image = (ImageView) itemView.findViewById(R.id.ad_image);
            int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
            ViewGroup.LayoutParams layoutParams = ad_image.getLayoutParams();
            layoutParams.width = widthPixels;
            layoutParams.height = layoutParams.width * 365 / 720;
        }
    }
}
